# coding: utf-8
"""
# 安装 redis 模块
pip install redis
"""
from redis import StrictRedis


class RedisDemo:
    def __init__(self, host):
        # 连接数据库
        self.sr = StrictRedis(host=host)

    def _test_string_add(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_string_add()
        """
        self.sr.set('str1', '1')
        print(self.sr.get('str1'))

    def _test_string_select(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_string_select()
        """
        print(self.sr.type('str1'))
        print(self.sr.keys('str1'))
        print(self.sr.get('str1'))

    def _test_string_update(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_string_update()
        """
        self.sr.set('str1', '2')
        print(self.sr.get('str1'))

    def _test_string_delete(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_string_delete()
        """
        self.sr.delete('str1')
        print(self.sr.get('str1'))

    def _test_hash_add(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_hash_add()
        """
        self.sr.hset('hash1', 'key1', 'val1')
        print(self.sr.hget('hash1', 'key1'))

    def _test_hash_select(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_hash_select()
        """
        print(self.sr.type('hash1'))
        print(self.sr.keys('hash1'))
        print(self.sr.hkeys('hash1'))
        print(self.sr.hvals('hash1'))
        print(self.sr.hget('hash1', 'key1'))
        print(self.sr.hmget('hash1', 'key1'))

    def _test_hash_update(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_hash_update()
        """
        self.sr.hset('hash1', 'key1', 'val2')
        print(self.sr.hget('hash1', 'key1'))
        self.sr.hmset('hash1', {'key1': 'val3'})
        print(self.sr.hmget('hash1', 'key1'))
        self.sr.hmset('hash1', {'key1': str(['val4', 'val5'])})
        print(self.sr.hmget('hash1', 'key1'))

    def _test_hash_delete(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_hash_delete()
        """
        self.sr.hdel('hash1', 'key1')
        print(self.sr.hget('hash1', 'key1'))
        self.sr.delete('str1')
        print(self.sr.get('str1'))

    def _test_list_add(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_list_add()
        """
        self.sr.lpush('list1', 'lpush')
        print(self.sr.lrange('list1', 0, -1))
        self.sr.rpush('list1', 'rpush')
        print(self.sr.lrange('list1', 0, -1))
        self.sr.linsert('list1', 'before', 'lpush', 'before')
        print(self.sr.lrange('list1', 0, -1))
        self.sr.rpush('list1', str({'key1': 'val1'}))
        print(self.sr.lrange('list1', 0, -1))

    def _test_list_select(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_list_select()
        """
        print(self.sr.lrange('list1', 0, -1))

    def _test_list_update(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_list_update()
        """
        self.sr.lset('list1', 0, 0)
        print(self.sr.lrange('list1', 0, -1))

    def _test_list_delete(self):
        """
        rd = RedisDemo("192.168.43.158")
        rd._test_list_delete()
        """
        self.sr.lrem('list1', '10', 'lpush')
        print(self.sr.lrange('list1', 0, -1))
        self.sr.delete('list1')
        print(self.sr.lrange('list1', 0, -1))


if __name__ == '__main__':
    rd = RedisDemo("192.168.43.158")
    rd._test_list_delete()
